home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 133 / c / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-17  |  15.9 KB  |  300 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:     list.c - Print a listing of a file.                          */
  4. /*                                                                          */
  5. /* Programmer: George R. Woodside                                           */
  6. /*                                                                          */
  7. /* Date:       October 25, 1986                                             */
  8. /*                                                                          */
  9. /* Flags:      -a          Set printer for pica spacing (10 cpi)            */
  10. /*             -b          Set printer for elite (12 cpi)                   */
  11. /*             -c          Set printer for compressed spacing (16 cpi)      */
  12. /*             -f          Turns off folding of long lines                  */
  13. /*             -n          Turns off numbering of lines.                    */
  14. /*             -w          Forces a wait for keypress after each page.      */
  15. /*                                                                          */
  16. /****************************************************************************/
  17.  
  18. #include <stdio.h>
  19. #include <osbind.h>
  20.  
  21. #define SLIMIT 23                       /* screen lines per page limit      */
  22. #define PLIMIT 60                       /* printer lines per page           */
  23. #define SWIDTH 78                       /* screen width                     */
  24. #define AWIDTH 78                       /* printer width                    */
  25. #define BWIDTH 94                       /* printer width                    */
  26. #define CWIDTH 134                      /* printer width                    */
  27. #define ATYPE "\033N"                   /* typesize control - pica          */
  28. #define BTYPE "\033E"                   /* typesize control - elite         */
  29. #define CTYPE "\033Q"                   /* typesize control - compress      */
  30. #define SFF "\033H\033J"                /* form feed on screen              */
  31. #define PFF "\014"                      /* form feed on printer             */
  32.  
  33. extern  char  *optarg;                  /* option argument                  */
  34. extern  int    optind;                  /* argument index                   */
  35.  
  36. int     width=SWIDTH;                   /* line width this time             */
  37. int     limit=SLIMIT;                   /* page length this time            */
  38. int     ccount;                         /* characters on line counter       */
  39. int     fcount;                         /* line count (in file)             */
  40. int     lcount;                         /* line count (on page)             */
  41. int     pcount;                         /* page counter                     */
  42.  
  43. int     fflag=1;                        /* fold long lines                  */
  44. int     nflag=1;                        /* line number flag                 */
  45. int     pflag=0;                        /* enable printer format flag       */
  46. int     wflag=0;                        /* wait at the end of pages         */
  47.  
  48. char    fname[30];                      /* File name                        */
  49. char    *flit="File: ";                 /* heading literal                  */
  50. char    date[9];                        /* date of listing                  */
  51. char    time[9];                        /* time of listing                  */
  52. char    *pname = "list";                /* our own name                     */
  53.  
  54. /****************************************************************************/
  55. /* Begin here                                                               */
  56. /****************************************************************************/
  57.  
  58. main(argc,argv)
  59. int     argc;                           /* argument count                   */
  60. char    **argv;                         /* argument pointers                */
  61. {
  62.   FILE    *f1;                          /* File handler functions           */
  63.   register  int  c;                     /* current data byte                */
  64.  
  65. #ifdef UNIX
  66.   pname = argv[0];                      /* name is always first parameter.  */
  67. #endif
  68.  
  69.   if(argc==1)                           /* if no arguments, print uasge     */
  70.     {
  71.       printf("List Utility 1.2         April 3, 1986\n");
  72.       printf("C. Itoh M-8510 Version.\n\n");
  73.       printf("usage: list [-abcfnw] filename\n\n");
  74.       printf("where:\n");
  75.       printf("       -a sets printer for pica (10 cpi)\n");
  76.       printf("       -b sets printer for elite (12 cpi)\n");
  77.       printf("       -c sets printer for compressed (17 cpi)\n");
  78.       printf("       -f turns off folding of long lines\n");
  79.       printf("       -n turns off line numbers\n");
  80.       printf("       -w turns on a keypress wait after each page\n");
  81.       exit(1);
  82.     }
  83.  
  84.   while((c = getopt(argc,argv,"ABCFNWabcfnw")) != EOF )
  85.     {
  86.       switch(c)
  87.         {
  88.           case 'A':
  89.           case 'a':
  90.             pflag=1;                    /* printer on                       */
  91.             limit=PLIMIT;               /* set page size                    */
  92.             Fforce(1,-3);               /* stdout to printer                */
  93.             printf(ATYPE);              /* set print type                   */
  94.             width=AWIDTH;               /* set line width                   */
  95.             break;                      /* and exit                         */
  96.  
  97.           case 'B':
  98.           case 'b':
  99.             pflag=1;                    /* printer on                       */
  100.             limit=PLIMIT;               /* set page size                    */
  101.             Fforce(1,-3);               /* stdout to printer                */
  102.             printf(BTYPE);              /* set print type                   */
  103.             width=BWIDTH;               /* set line width                   */
  104.             break;                      /* and exit                         */
  105.  
  106.           case 'C':
  107.           case 'c':
  108.             pflag=1;                    /* printer on                       */
  109.             limit=PLIMIT;               /* set page size                    */
  110.             Fforce(1,-3);               /* stdout to printer                */
  111.             printf(CTYPE);              /* set print type                   */
  112.             width=CWIDTH;               /* set line width                   */
  113.             break;                      /* and exit                         */
  114.  
  115.           case 'F':
  116.           case 'f':
  117.             fflag=0;                    /* folding off                      */
  118.             break;                      /* and exit                         */
  119.  
  120.           case 'N':
  121.           case 'n':
  122.             nflag=0;                    /* numbering off                    */
  123.             break;                      /* and exit                         */
  124.  
  125.           case 'W':
  126.           case 'w':
  127.             wflag=1;                    /* wait on                          */
  128.             break;
  129.         }                               /* end switch                       */
  130.     }                                   /* end arguments parsed             */
  131.  
  132.   gtime(time);                          /* read the time                    */
  133.   gdate(date);                          /* and the date                     */
  134.  
  135.   if(optind != argc)                    /* if file names entered,           */
  136.     {
  137.       while(optind != argc)
  138.         {
  139.           if((f1 = fopen(argv[optind], "r")) == NULL) /* if open fails,     */
  140.             {
  141.               printf("%s:Unable to open file %s\n",pname,argv[optind++]);
  142.               continue;                 /* punt                             */
  143.             }
  144.           else                          /* if open worked,                  */
  145.             {
  146.               strcpy(fname,flit);       /* get the header literal           */
  147.               strcat(fname,argv[optind++]); /* add the file name            */
  148.               do_list(f1);              /* list the file                    */
  149.             }                           /* end good file open               */
  150.         }                               /* end all files                    */
  151.     }                                   /* end file names entered           */
  152.   else                                  /* if no file name entered,         */
  153.     do_list(stdin);                     /* list standard in                 */
  154.  
  155.   printf("\014");                       /* issue form feed                  */
  156.   Fforce(1,-1);                         /* stdout to console, just in case  */
  157. }                                       /* end main                         */
  158.  
  159. /****************************************************************************/
  160. /* Do listing of one file                                                   */
  161. /****************************************************************************/
  162. do_list(fp)
  163. FILE  *fp;
  164. {
  165.  
  166.   register  int  c;                     /* data byte                        */
  167.  
  168.   ccount = 0;                           /* characters on line               */
  169.   fcount = 0;                           /* line in file is zero             */
  170.   lcount = 1000;                        /* insure leading form feed         */
  171.   pcount = 0;                           /* clear page count                 */
  172.  
  173.   lcount=test_tof(lcount);              /* reset the line count             */
  174.  
  175.   if(nflag)                             /* if numbering lines,              */
  176.     {
  177.       printf("%4d: ",++fcount);         /* print the count                  */
  178.       ccount=5;                         /* and set the column               */
  179.     }
  180.  
  181.   while((c = getc(fp)) != EOF)
  182.     {
  183.       if(c=='\n')                       /* if a newline,                    */
  184.         {
  185.           putchar(c);                   /* first, print it                  */
  186.           lcount=test_tof(++lcount);    /* Heading if time                  */
  187.           if(lcount==-1)                /* if user cancelled us,            */
  188.             return(1);                  /* punt                             */
  189.           ccount=0;                     /* clear column                     */
  190.           if(nflag)                     /* if numbering,                    */
  191.             {
  192.               printf("%4d: ",++fcount); /* numbering on                     */
  193.               ccount=5;                 /* and set the column               */
  194.             }                           /* end numbering mode               */
  195.         }                               /* end newline character            */
  196.       else                              /* a data character                 */
  197.         {
  198.           if(c=='\t')                   /* if a tab,                        */
  199.             {
  200.               bytout(' ');              /* insure one space                 */
  201.               while(ccount&7)           /* and enough to move               */
  202.                 bytout(' ');            /* to next multiple                 */
  203.             }                           /* end tab character                */
  204.           else                          /* must be normal data              */
  205.             bytout(c);                  /* so print it                      */
  206.         }                               /* end normal character             */
  207.     }                                   /* end of a file                    */
  208.   return(0);                            /* show ended ok                    */
  209. }                                       /* end main                         */
  210.  
  211. /****************************************************************************/
  212. /* Write one data byte to output                                            */
  213. /****************************************************************************/
  214. bytout(c)
  215. char    c;
  216. {
  217.   if(++ccount>width)                    /* if at right margin               */
  218.     {
  219.       if(fflag)                         /* if folding lines,                */
  220.         {
  221.           ccount=1;                     /* column is back to 1              */
  222.           printf("\n");                 /* down one line                    */
  223.           if(nflag)                     /* if numbering,                    */
  224.             printf("      ");           /* keep text aligned                */
  225.           putchar(c);                   /* now print the character          */
  226.           lcount++;                     /* and count the line               */
  227.         }                               /* end folding mode                 */
  228.     }                                   /* end at right margin              */
  229.   else
  230.     putchar(c);                         /* not at margin, just print        */
  231. }
  232.  
  233. /****************************************************************************/
  234. /* Test top of form, and print heading if needed                            */
  235. /****************************************************************************/
  236.  
  237. test_tof(lcount)
  238. int     lcount;                         /* Line count                       */
  239. {
  240.   register  int  c;
  241.   if(lcount>=limit)                     /* if at bottom of page             */
  242.     {
  243.       if((pflag==0)&(pcount>1)&(wflag)) /* if waiting, and past first page  */
  244.         {
  245.           c=Crawcin();                  /* get a keypress                   */
  246.           if(c=='\003')                 /* if it is a cancel (^C)           */
  247.             return(-1);                 /* say we must die                  */
  248.         }
  249.       if(pflag)                         /* if on a printer,                 */
  250.         printf(PFF);                    /* New page (printer)               */
  251.       else                              /* if at the console                */
  252.         {
  253.           if(wflag)                     /* if in wait mode,                 */
  254.             printf(SFF);                /* Clear screen                     */
  255.         }
  256.  
  257.       if(pflag || wflag)                /* if printing or waiting           */
  258.         {
  259.           printf("%-30s          ",fname); /* print file name               */
  260.           printf("%s %s          ",date,time); /* now time and date         */
  261.           printf("Page: %4d\n\n",pcount++); /* and page number              */
  262.         }
  263.       lcount=1;                         /* reset the line count             */
  264.     }                                   /* end bottom of page               */
  265.   return(lcount);                       /* return the line number           */
  266. }
  267.  
  268. /****************************************************************************/
  269. /* Get time into ASCII string                                               */
  270. /****************************************************************************/
  271. gtime(str)
  272. char    *str;
  273. {
  274.   register  unsigned  int  temp;
  275.   register  unsigned  int  hh,mm,ss;
  276.  
  277.   temp=Tgettime();                      /* read the system time             */
  278.   hh = (temp>>11) & 0x001f;             /* extract the hour                 */
  279.   mm = (temp>>5)  & 0x003f;             /* extract the minutes              */
  280.   ss = (temp<<1)  & 0x003f;             /* extract the seconds              */
  281.   sprintf(str,"%2d:%02d:%02d",hh,mm,ss);/* format the string                */
  282. }
  283.  
  284. /****************************************************************************/
  285. /* Get date into ASCII string                                               */
  286. /****************************************************************************/
  287. gdate(str)
  288. char    *str;
  289. {
  290.   register  unsigned  int  temp;
  291.   register  unsigned  int  mm,dd,yy;
  292.  
  293.   temp=Tgetdate();
  294.   mm  = (temp>>5)  & 0x000f;            /* get the month                    */
  295.   dd  = temp       & 0x1f;              /* and the day                      */
  296.   yy  = (temp>>9)  & 0x007f;            /* get the year increment           */
  297.   yy += 80;                             /* add the base year                */
  298.   sprintf(str,"%2d/%02d/%02d",mm,dd,yy);/* format the string                */
  299. }
  300.